<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="会员ID" prop="memberUserId">
        <el-input
          v-model="queryParams.memberUserId"
          placeholder="请输入会员ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流水类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>
          <el-option label="消费" value="0" />
          <el-option label="充值" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['member:walletLog:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['member:walletLog:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="walletLogList">
      <el-table-column label="流水ID" align="center" prop="memberWalletLogId" width="100" />
      <el-table-column label="会员ID" align="center" prop="memberUserId" width="100" />
      <el-table-column label="金币数量" align="center" prop="gold" width="120">
        <template #default="scope">
          <span :style="{color: scope.row.type === '1' ? 'green' : 'red'}">
            {{ scope.row.type === '1' ? '+' : '-' }}{{ scope.row.gold }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="type" width="100">
        <template #default="scope">
          <el-tag v-if="scope.row.type === '0'" type="danger">消费</el-tag>
          <el-tag v-else-if="scope.row.type === '1'" type="success">充值</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="View"
            @click="handleView(scope.row)"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或查看对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="walletLogRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="会员ID" prop="memberUserId">
          <el-input v-model="form.memberUserId" placeholder="请输入会员ID" :disabled="form.memberWalletLogId != null" />
        </el-form-item>
        <el-form-item label="金币数量" prop="gold">
          <el-input-number v-model="form.gold" controls-position="right" :min="1" :disabled="form.memberWalletLogId != null" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-radio-group v-model="form.type" :disabled="form.memberWalletLogId != null">
            <el-radio label="0">消费</el-radio>
            <el-radio label="1">充值</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :disabled="form.memberWalletLogId != null" />
        </el-form-item>
        <el-form-item label="创建时间" v-if="form.memberWalletLogId != null">
          <span>{{ parseTime(form.createTime) }}</span>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button v-if="form.memberWalletLogId == null" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">{{ form.memberWalletLogId != null ? '关 闭' : '取 消' }}</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="MemberWalletLog">
import { listWalletLog, getWalletLog, addWalletLog } from "@/api/member/walletLog";
import { getCurrentInstance, ref, reactive, toRefs } from 'vue';

const { proxy } = getCurrentInstance();
const { parseTime } = proxy;

const walletLogList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    memberUserId: null,
    type: null
  },
  rules: {
    memberUserId: [
      { required: true, message: "会员ID不能为空", trigger: "blur" }
    ],
    gold: [
      { required: true, message: "金币数量不能为空", trigger: "blur" }
    ],
    type: [
      { required: true, message: "类型不能为空", trigger: "change" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询钱包流水列表 */
function getList() {
  loading.value = true;
  listWalletLog(queryParams.value).then(response => {
    walletLogList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    memberWalletLogId: null,
    memberUserId: null,
    gold: null,
    type: "1",
    remark: null
  };
  proxy.resetForm("walletLogRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加钱包流水";
}

/** 查看按钮操作 */
function handleView(row) {
  reset();
  getWalletLog(row.memberWalletLogId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "钱包流水详情";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["walletLogRef"].validate(valid => {
    if (valid) {
      addWalletLog(form.value).then(response => {
        proxy.$modal.msgSuccess("新增成功");
        open.value = false;
        getList();
      });
    }
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('member/walletLog/export', {
    ...queryParams.value
  }, `钱包流水_${new Date().getTime()}.xlsx`);
}

getList();
</script>
